package com.icss.ah.vehicleUsers.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.vehicleUsers.bean.CarUserBean;
/**
 * 车辆与配送人员关系维护
 * @author wangjun
 *
 */
@Controller
@Scope("prototype")
public class VehicleUsersAction  extends BaseAction{
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	public String toVehicleList(){
		request = this.getRequest();
		return "toVehicleList";
	}
	/**
	 * 查询车辆对应的人员关系列表
	 */
	public void queryVehicleUsersList(){
		request = this.getRequest();
		String result = "";
		try {
			UserInfoBean userInfo=getUserInfo("");
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			String carNo = request.getParameter("carNo");//车牌号（查询条件）
			carNo = carNo == null ? "" : carNo;
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);
			map.put("shopId",shopId);
			map.put("carNo",carNo);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<CarUserBean> vehicleUsersList = new ArrayList<CarUserBean>();
			vehicleUsersList = vehicleUsersService.queryVehicleUsersList(map);	
			long vehicleUsersCount = vehicleUsersService.queryVehicleUsersCount(map);//查询总记录数
			dataGridJson.setRows(vehicleUsersList);
			dataGridJson.setTotal(vehicleUsersCount); 
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryVehicleUsersList|查询车辆与配送人员关系列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryVehicleUsersList|查询车辆与配送人员关系列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	/**
	 *进入配送人员列表界面
	 * @return
	 */
	public String assignUsers(){
		request = this.getRequest();
		try {
			UserInfoBean userInfo=getUserInfo("");
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			String carUuid = request.getParameter("carUuid");//车辆Uuid
			carUuid = carUuid == null ? "" : carUuid; 
			String flag = request.getParameter("flag");//详情或修改标识
			flag = flag == null ? "" : flag; 
			HashMap map = new HashMap();
			map.put("corpId",corpId);
			map.put("shopId",shopId);
			map.put("carUuid",carUuid);
			List usersList = new ArrayList();
			if(flag.equals("edit")){//为选择分配人员时，查询人员列表
				usersList = vehicleUsersService.queryUsersList(map);
			}else if(flag.equals("detail")){//为查看详情时，查询人员列表
				usersList = vehicleUsersService.queryUsersDetail(map);
			}
			request.setAttribute("usersList", usersList);
			request.setAttribute("carUuid",carUuid );
			request.setAttribute("flag", flag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toVehicleEdit";
	}
	/**
	 * 保存车辆对应人员关系
	 * @return
	 */
	public String saveRelation() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			boolean revl = false;
			UserInfoBean userInfo=getUserInfo("");
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			String carUuid = request.getParameter("carUuid");//车辆Uuid
			carUuid = carUuid == null ? "" : carUuid; 
			String userIds = request.getParameter("userIds");//选择的配送人员uuid
			userIds = userIds == null ? "" : userIds; 
			HashMap map = new HashMap();
			map.put("carUuid",carUuid);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			int f = vehicleUsersService.deleteRelation(map);//删除当前车辆对应的配送人员
			if(f>=0){
				String[] userIdArray = userIds.split(",");
				for(int i=0;i<userIdArray.length;i++){
					if(!"".equals(userIdArray[i])){
						map.put("userId", userIdArray[i]);
						revl =  vehicleUsersService.saveRelation(map);//新增车辆对应的人员
					}
				}
			}
			if(revl){
				jsonText.setMsg("保存成功！");
				jsonText.setSuccess(true);
			}else{
				jsonText.setMsg("保存失败！");
				jsonText.setSuccess(false);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			jsonText.setMsg("保存失败！");
			jsonText.setSuccess(false);
		}
		return "saveRelate";
	}
	
	
}
